package com.wxprogram.smallprogram.service.impl;

import com.wxprogram.smallprogram.dao.IFindstudent;
import com.wxprogram.smallprogram.dao.IFindteacher;
import com.wxprogram.smallprogram.dao.MajorDao;
import com.wxprogram.smallprogram.entity.FindStudent;
import com.wxprogram.smallprogram.entity.FindTeacher;
import com.wxprogram.smallprogram.entity.utilEntity.PageUtil;
import com.wxprogram.smallprogram.service.FileService;
import com.wxprogram.smallprogram.service.IStudentDeliverReqService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class StudentDeliverReqServiceImpl implements IStudentDeliverReqService {


	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private IFindteacher iFindteacher;

	@Autowired
	private IFindstudent iFindstudent;

	@Autowired
	private MajorDao majorDao;

	@Resource
	private RedisTemplate<String,Object> redisTemplate;


	/**
	 * 插入学生信息
	 * 
	 * @param findTeacher
	 * @return
	 */
	public int insertFindTeacher(FindTeacher findTeacher) {
		//根据专业获取类型
		String major = findTeacher.getStumajor();
		findTeacher.setStumajortype(majorDao.selectMajorTypeByMajor(major));
		int stucode = findTeacher.getStucode();
		String key="findTeacherCode_"+stucode;
		ValueOperations<String, Object> operations = redisTemplate.opsForValue();
		boolean hasKey = redisTemplate.hasKey(key);
		if(hasKey){
			return iFindteacher.insertFindteacher(findTeacher);
		}else{
			operations.set(key,findTeacher);
            logger.info("插入缓存:"+key);
			return iFindteacher.insertFindteacher(findTeacher);
		}

	}


	/**
	 * 根据openID查询所有发布
	 */

	@Override
	public List<FindTeacher> getStudentPulish(String openID) {
		List<FindTeacher> list1 = new ArrayList<>();
		List<Integer> list2 = new ArrayList<>();
		list2=iFindteacher.selectStuCodeByOpenid(openID);
		ValueOperations<String, Object> operations = redisTemplate.opsForValue();
		for(Integer StuCode:list2){
			FindTeacher findTeacher1= (FindTeacher) operations.get("findTeacherCode_"+StuCode);
			if(findTeacher1 != null) {
				logger.info("从缓存中获取数据: findTeacherCode_" + StuCode);
				list1.add(findTeacher1);
			}
		}
		if(list1.size() == list2.size() ) {
			logger.info("从缓存获取数据成功");
			return list1;
		}else{
			logger.info("缓存无数据，从数据库中获取");
			return iFindteacher.selectFindteacherByOpenid(openID);
		}
	}
	
	/**
	 * 根据条件分页查询
	 */
	@Override
	public PageUtil<FindStudent> getDeliver(String city, String school, String majortype, String major, int current) {
		FindStudent findstudent = new FindStudent();
		PageUtil<FindStudent> page = new PageUtil<>(current);
		findstudent.setTeachercity(city);
		findstudent.setTeacherschool(school);
		findstudent.setTeachermajortype(majortype);
		findstudent.setTeachermajor(major);
		int count = iFindstudent.getCount(findstudent);
		page.setCount(count);
		page.setList(iFindstudent.selectFindstudentList(findstudent,page.getOffset(),page.getSize()));
		System.out.println("Service>>>>>总条数" + count + ",发布：" + iFindstudent.selectFindstudentList(findstudent,page.getOffset(),page.getSize()));
		return page;

	}

	/**
	 * 获取详细信息
	 */
	@Override
	public FindStudent getDetail(int code){
		return iFindstudent.selectFindstudentByTeachercode(code);
	}

	/**
	 * 根据code删除发布信息
	 * @param code
	 * @return
	 */
	@Override
	public int deletePub(int code) {
		String key = "findTeacherCode_"+ code;
		ValueOperations<String, Object> operations = redisTemplate.opsForValue();
		boolean hasKey = redisTemplate.hasKey(key);
		if(hasKey){
			redisTemplate.delete(key);
			logger.info("更新用户时，从缓存中删除用户 >> " +key);
		}

		int count = iFindteacher.deleteFindteacherByCode(code);

//		if (count > 0){
//			final int _code = code;
//			new Thread(new Runnable(){
//				@Autowired
//				private FileService fileService;
//
//				@Override
//				public void run() {
//					String path = iFindteacher.selectImgByCode(_code);
//					fileService.deleteFile(path);
//				}
//			}).start();
//		}

		return count;
	}

	/**
	 * 修改查看数
	 *
	 * @param code
	 * @return
	 */
	@Override
	public int updateViewNum(int code) {
		return iFindteacher.updateViewNum(code);
	}

}
